import CreateTopic from '@/views/CreateTopic/CreateTopic.vue'
import Home from '@/views/Home/Home.vue'
import HotPage from '@/views/HotPage/HotPage.vue'
import Login from '@/views/Login/Login.vue'
import PersonalCenter from '@/views/PersonalCenter/PersonalCenter.vue'
import FollowPostList from '@/views/PostListViews/FollowPostList/FollowPostList.vue'
import HomePostList from '@/views/PostListViews/HomePostList/HomePostList.vue'
import SearchPage from '@/views/PostListViews/SearchPage/SearchPage.vue'
import TopicPage from '@/views/PostListViews/TopicPage/TopicPage.vue'
import ForumStatistics from '@/views/TopicExplore/ForumStatistics.vue'
import Submit from '@/views/Submit/Submit.vue'
import EditTopic from '@/views/Submit/editTopic.vue'
import Test from '@/views/Test/Test.vue'
import TopicExplore from '@/views/TopicExplore/TopicExplore.vue'
import TopicSetting from '@/views/TopicSetting/TopicSetting.vue'
import UserHome from '@/views/UserHome/UserHome.vue'
import reportHome from '@/views/reportHome/reportHome.vue'
import PostDetail from '@/views/PostDetail/PostDetail.vue'
import MeetingReserve from '@/views/Meeting/MeetingReserve.vue'
import ExpertIndex from '@/views/Expert/ExpertIndex.vue'
import SmartIndex from '@/views/Smart/SmartIndex.vue'
import ExpertDetail from '@/views/Expert/ExpertDetail.vue'
import Question from '@/views/Question/Question.vue'
import ExpertStatisticsIndex from '@/views/Expert/ExpertStatisticsIndex.vue'
import DetectionPage from "@/views/detectionPage/detectionPage.vue"
import SubmitSuccess from "@/views/Submit/components/SubmitSuccess.vue"



import { createRouter, createWebHistory } from 'vue-router'
import TopicExploreItem from '@/views/TopicExplore/TopicExploreItem.vue'
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: Home,
      // component: Home,
      redirect: '/home',
      children: [
        {
          path: '/topicExplore',
          component: HomePostList,
          children: [
            {
              path: 'post/:id',
              component: PostDetail,
            },
          ],
        },
        {
          path: '/followPost',
          component: FollowPostList,
          children: [
            {
              path: 'post/:id',
              component: PostDetail,
            },
          ],
        },
        {
          path: '/home',
          component: TopicExplore,
          // children: [
          //   {
          //     path: 'post/:id',
          //     component: PostDetail,
          //   },
          // ],
        },
        {
          path: '/topicExploreItem/:id',
          component: TopicExploreItem,
          // children: [
          //   {
          //     path: 'post/:id',
          //     component: PostDetail,
          //   },
          // ],
        },
        {
          path: '/home/post/:id',
          component: PostDetail,
          children: [
            // {
            //   path: 'post/:id',
            //   component: PostDetail,
            // },
          ],
        },
        {
          path: '/forumStatistics',
          component: ForumStatistics,
        },
        {
          path: '/topic/:topicId',
          component: TopicPage,
          children: [
            {
              path: 'post/:id',
              component: PostDetail,
            },
            {
              path: 'discuss/:discussId',
              component: TopicPage,
              children: [
                {
                  path: 'post/:id',
                  component: PostDetail,
                },
              ],
            },
          ],
        },
        {
          path: '/hotPost',
          component: HotPage,
          children: [
            {
              path: 'post/:id',
              component: PostDetail,
            },
          ],
        },
        {
          path: '/person',
          component: PersonalCenter,
        },
        {
          path: '/submit',
          component: Submit,
        },
        {
          path: '/editTopic/:id',
          component: EditTopic,
        },
        {
          path: '/createTopic',
          component: CreateTopic,
        },
        {
          path: '/topicSetting/:topicId',
          component: TopicSetting,
        },
        {
          path: '/search/:keyword',
          component: SearchPage,
          children: [
            {
              path: 'post/:id',
              component: PostDetail,
            },
          ],
        },
        {
          path: '/user/:uid',
          component: UserHome,
          children: [
            {
              path: 'post/:id',
              component: PostDetail,
            },
          ],
        },
        {
          path: '/report/:uid',
          component: reportHome,
          children: [
            {
              path: 'post/:id',
              component: PostDetail,
            },
          ],

        },
        {
          path: '/question',
          component: Question,
          meta: {
            keepAlive: true // 启用缓存
          }
        },
        {
          path: '/question/:id',
          component: () => import('../views/Question/QuestionDetail.vue'),
          meta: {
            from: 'list' // 标记来源
          }
        },
        {
          path: '/meeting',
          component: MeetingReserve,
        },
        {
          path: '/expert',
          component: ExpertIndex,
        },
        {
          path: '/smart',
          component: SmartIndex,
        },
        {
          path: '/submitSuccess',
          component: SubmitSuccess,
        },
        {
          path: '/expertStatisticsIndex',
          component: ExpertStatisticsIndex,
        },
        {
          path: '/expert/:id',
          name: 'ExpertDetail',
          component: ExpertDetail,
        },
      ],
    },
    {
      path: '/login',
      component: Login,
    },
    {
      path: '/meeting/webview',
      name: 'MeetingWebview',
      component: () => import('../views/Meeting/MeetingWebviewDialog.vue')
    },
    {
      path: '/test',
      component: Test,
    },
    {
      path: '/detectionPage',
      component: DetectionPage
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    // 如果是从问题详情返回列表，不要自动滚动
    if (from.path.startsWith('/question/') && to.path === '/question') {
      return false
    }

    const mainContent = document.querySelector('main')
    if (mainContent) {
      mainContent.scrollTop = 0
    }

    if (savedPosition) {
      return savedPosition
    }

    if (to.meta.scrollBehavior) {
      // return to.meta.scrollBehavior
    }

    return { top: 0 }
  }
})

router.beforeEach((to, from, next) => {
  // 检查是否是/home路由且携带token参数
  if (to.path === '/home' && to.query.token) {
    next({
      path: '/detectionPage',
      query: { token: to.query.token }
    })
    return
  }

  if (!router.options.scrollBehavior) {
    window.scrollTo(0, 0)
  }
  next()
})

router.afterEach(() => {
  const mainContent = document.querySelector('main')
  if (mainContent) {
    mainContent.scrollTop = 0
  }
})

export default router
